/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author Abahe
 */
public class BTIMidlet extends MIDlet {

    //---------------------------------------------------
    //class
    private SplashScreen splashScreen;
    private MenuCanvas menuCanvas;    
    private ObjekWisata ow;
    private ObjekWisataList owList;
    private TempatKuliner tk;
    private TempatKulinerList tkList;
    private Hotel ht;
    private HotelList htList;
    private Peta peta;
    private Pilihan pilihan;
    private Help help;


    private Display display;

    //---------------------------------------------------
    //status
    public static int BEFORE = 0;
    public static int INDEKS = 0;

    //---------------------------------------------------
    //class
    private RecordStore rs;
    private RecordEnumeration re;
    public static String LANG = "ID";
    public static int WIDTH = 0;
    public static int HEIGHT = 0;
    private boolean empty = true;

    public BTIMidlet() {
        display = Display.getDisplay(this);

        rs = null;
        peta = new Peta(this);        
        ow = new ObjekWisata(this, "Objek Pariwisata", 1);
        tk = new TempatKuliner(this, "Tempat Kuliner", 1);
        ht = new Hotel(this, "Hotel", 1);
        pilihan = new Pilihan(this, "Pilihan");
        help = new Help(this, "Help");
        
        //membuat atau membuka record sotre
        try {
            rs = RecordStore.openRecordStore("rsBTI", true);
        } catch (RecordStoreException rse) {
            System.exit(1);
        }
    }

    public void startApp() {
        splashScreen = new SplashScreen(this);
        display.setCurrent(splashScreen);
        splashScreen.start();

        getSettings();
        if (empty = true) {
            setSetting("EN");
        }
        getSettings();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void showMenuCanvas() {
        if (menuCanvas == null) {
            menuCanvas = new MenuCanvas(this);
            display.setCurrent(menuCanvas);
            menuCanvas.start();
        }
    }

    public void backToMenuCanvas() {
        menuCanvas.start();
        display.setCurrent(menuCanvas);

    }

    public void exit() {
        menuCanvas.stop();
        menuCanvas = null;
        destroyApp(true);
        notifyDestroyed();
    }

    public void setSetting(String s) {
        byte[] temp = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeUTF(s);
            temp = baos.toByteArray();
        } catch (IOException ioe) {
        }

        try {
            rs.addRecord(temp, 0, temp.length);
            empty = false;
        } catch (RecordStoreNotOpenException rsnoe) {
        } catch (RecordStoreException rse) {
        }
    }

    public void getSettings() {
        byte[] temp = null;
        try {
            re = rs.enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                int i = re.nextRecordId();
                temp = rs.getRecord(i);
                ByteArrayInputStream bais = new ByteArrayInputStream(temp);
                DataInputStream dis = new DataInputStream(bais);
                try {
                    LANG = dis.readUTF();
                    empty = false;
                } catch (IOException ioe) {
                }
            }
        } catch (InvalidRecordIDException invID) {
        } catch (RecordStoreNotOpenException rsnoe) {
        } catch (RecordStoreException rse) {
        }

        System.out.println("language : " + LANG);
    }

    public void showNotSupported() {
        //splashScreen.stop();
        menuCanvas.stop();
        menuCanvas = null;
        splashScreen = null;
        NotSupportedCanvas notSupport = new NotSupportedCanvas(this);
        display.setCurrent(notSupport);
    }

    public void showObjekWisataList() {
        menuCanvas.stop();
        owList = new ObjekWisataList(this, display, "Objek Pariwisata");
        display.setCurrent(owList);
    }

    public void showObjekWisata(int indeks){
        ow.deleteAll();
        ow = null;
        ow = new ObjekWisata(this, "Objek Wisata", indeks);
        ow.addItem(indeks);
        display.setCurrent(ow);
    }

    public void showPeta(){
        //ow = null;
        menuCanvas.stop();
        display.setCurrent(peta);
    }

    public void showHelp() {        
        display.setCurrent(help);
    }

    public void showTempatKuliner() {
        
    }

    public void showHotelList() {
        menuCanvas.stop();
        htList = new HotelList(this, display, "Hotel");
        display.setCurrent(htList);
    }
    
    public void showHotel(int indeks) {
        ht.deleteAll();
        ht = null;
        ht = new Hotel(this, "Hotel", indeks);
        ht.addItem(indeks);
        display.setCurrent(ht);
    }

    public void showPilihan() {
        
    }

    public void showTempatKuliner(int INDEKS) {
        
    }
}
